from app.extensions import db


class ImageRecord(db.Model):
    __tablename__ = 'image_records'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False, comment='关联用户 id')
    image_url = db.Column(db.String(255), comment='图片保存路径')
    predicted_class = db.Column(db.JSON, comment='识别出的分类结果')
    created_at = db.Column(db.DateTime, server_default=db.func.now(), comment='上传时间')

    def to_dict(self):
        return {
            'id': self.id,
            'user_id': self.user_id,
            'username': self.user.username if self.user else None,
            'image_url': self.image_url,
            'predicted_class': self.predicted_class,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S')
        }
